<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
      class="query-params-form"
    >
      <el-form-item label="菜单编码" prop="menuNumber">
        <el-input
          v-model="queryParams.menuNumber"
          placeholder="请输入菜单编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="菜单名称" prop="menuName">
        <el-input
          v-model="queryParams.menuName"
          placeholder="请输入菜单名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="一级分组名称" prop="firstgroupName">
        <el-input
          v-model="queryParams.firstgroupName"
          placeholder="请输入一级分组名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="二级分组名称" prop="secondgroupName">
        <el-input
          v-model="queryParams.secondgroupName"
          placeholder="请输入二级分组名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据状态" prop="dataState">
        <el-input
          v-model="queryParams.dataState"
          placeholder="请输入数据状态"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="禁用状态" prop="disabledState">
        <el-select
          v-model="queryParams.disabledState"
          placeholder="请选择禁用状态"
          clearable
        >
          <el-option
            v-for="dict in disabled_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建者" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建者"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['barcode:menu:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['barcode:menu:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['barcode:menu:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['barcode:menu:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="menuList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="Id" align="left" prop="id" width="55" />
      <el-table-column label="菜单编码" align="left" prop="menuNumber" />
      <el-table-column label="菜单名称" align="left" prop="menuName" />
      <el-table-column label="菜单图标" align="left" prop="menuIcon" />
      <el-table-column
        label="一级分组编码"
        align="left"
        prop="firstgroupNumber"
      />
      <el-table-column
        label="一级分组名称"
        align="left"
        prop="firstgroupName"
      />
      <el-table-column
        label="二级分组编码"
        align="left"
        prop="secondgroupNumber"
      />
      <el-table-column
        label="二级分组名称"
        align="left"
        prop="secondgroupName"
      />
      <el-table-column label="显示顺序" align="left" prop="showOrder" />
      <el-table-column label="数据状态" align="left" prop="dataState" />
      <el-table-column label="禁用状态" align="left" prop="disabledState">
        <template #default="scope">
          <dict-tag
            :options="disabled_status"
            :value="scope.row.disabledState"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column label="创建者" align="left" prop="createBy" /> -->
      <el-table-column
        label="创建时间"
        align="left"
        prop="createTime"
        width="160"
      />
      <!-- <el-table-column label="更新者" align="left" prop="updateBy" />
      <el-table-column label="更新时间" align="left" prop="updateTime" />
      <el-table-column label="备注" align="left" prop="remark" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="150px"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['barcode:menu:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['barcode:menu:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改条码菜单对话框 -->
    <el-dialog :title="title" v-model="open" width="520px" append-to-body>
      <el-form ref="menuRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="菜单编码" align="left" prop="menuNumber">
          <el-input v-model="form.menuNumber" placeholder="请输入菜单编码" />
        </el-form-item>
        <el-form-item label="菜单名称" prop="menuName">
          <el-input v-model="form.menuName" placeholder="请输入菜单名称" />
        </el-form-item>
        <el-form-item label="菜单图标" prop="menuIcon">
          <el-input v-model="form.menuIcon" placeholder="请输入菜单图标" />
        </el-form-item>
        <el-form-item label="一级分组编码" prop="firstgroupNumber">
          <el-input
            v-model="form.firstgroupNumber"
            placeholder="请输入一级分组编码"
          />
        </el-form-item>
        <el-form-item label="一级分组名称" prop="firstgroupName">
          <el-input
            v-model="form.firstgroupName"
            placeholder="请输入一级分组名称"
          />
        </el-form-item>
        <el-form-item label="二级分组编码" prop="secondgroupNumber">
          <el-input
            v-model="form.secondgroupNumber"
            placeholder="请输入二级分组编码"
          />
        </el-form-item>
        <el-form-item label="二级分组名称" prop="secondgroupName">
          <el-input
            v-model="form.secondgroupName"
            placeholder="请输入二级分组名称"
          />
        </el-form-item>

        <el-form-item label="显示排序" prop="showOrder">
          <el-input-number
            v-model="form.showOrder"
            controls-position="right"
            :min="0"
          />
        </el-form-item>
        <el-form-item label="数据状态" prop="dataState">
          <el-input v-model="form.dataState" placeholder="请输入数据状态" />
        </el-form-item>
        <el-form-item label="禁用状态" prop="disabledState">
          <!-- <el-select v-model="form.disabledState" placeholder="请选择禁用状态">
            <el-option
              v-for="dict in disabled_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select> -->

          <el-radio-group v-model="form.disabledState">
            <el-radio
              v-for="dict in disabled_status"
              :key="dict.value"
              :value="parseInt(dict.value)"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Menu">
import {
  listMenu,
  getMenu,
  delMenu,
  addMenu,
  updateMenu,
} from "@/api/barcode/menu";

const { proxy } = getCurrentInstance();
const { disabled_status } = proxy.useDict("disabled_status");

const menuList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    menuNumber: null,
    menuName: null,
    firstgroupName: null,
    secondgroupName: null,
    dataState: null,
    disabledState: null,
    createBy: null,
  },
  rules: {
    ID: [{ required: true, message: " 不能为空", trigger: "blur" }],
    menuNumber: [
      { required: true, message: "菜单编码不能为空", trigger: "blur" },
    ],
    menuName: [
      { required: true, message: "菜单名称不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询条码菜单列表 */
function getList() {
  loading.value = true;
  listMenu(queryParams.value).then((response) => {
    menuList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    ID: null,
    menuNumber: null,
    menuName: null,
    menuIcon: null,
    firstgroupNumber: null,
    firstgroupName: null,
    secondgroupNumber: null,
    secondgroupName: null,
    showOrder: 0,
    dataState: null,
    disabledState: 0,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
  };
  proxy.resetForm("menuRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加条码菜单";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _ID = row.id || ids.value;
  getMenu(_ID).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改条码菜单";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["menuRef"].validate((valid) => {
    if (valid) {
      if (form.value.ID != null) {
        updateMenu(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMenu(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _IDs = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除条码菜单编号为"' + _IDs + '"的数据项？')
    .then(function () {
      return delMenu(_IDs);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "barcode/menu/export",
    {
      ...queryParams.value,
    },
    `menu_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
